<template>
  <div class="model-selection-page">
    <header class="header">
      <h1>选择AI模型</h1>
      <div class="right-actions">
        <router-link to="/" class="back-button">
          <i class="fas fa-arrow-left"></i> 返回聊天
        </router-link>
      </div>
    </header>

    <div class="models-container">
      <div 
        v-for="model in availableModels" 
        :key="model.id" 
        class="model-card"
        :class="{ 'active': currentModelId === model.id }"
        @click="selectModel(model.id)"
      >
        <div class="model-info">
          <h3>{{ model.name }}</h3>
          <p v-if="model.hasDeepThinking" class="feature-tag">支持深度思考</p>
        </div>
        <div class="model-action">
          <button class="select-btn" :disabled="currentModelId === model.id">
            {{ currentModelId === model.id ? '当前使用中' : '选择' }}
          </button>
        </div>
      </div>

      <div v-if="availableModels.length === 0" class="no-models">
        <p>暂无可用模型，请稍后再试</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useChatStore } from '../store/chat'

const router = useRouter()
const chatStore = useChatStore()

// 计算当前使用的模型ID
const currentModelId = computed(() => chatStore.currentSession?.model || '')

// 可用模型列表
const availableModels = computed(() => chatStore.availableModels)

// 选择模型并创建新会话
function selectModel(modelId: string) {
  if (modelId !== currentModelId.value) {
    chatStore.createNewSession(modelId)
    router.push('/')
  }
}

onMounted(() => {
  // 确保模型列表已加载
  if (availableModels.value.length === 0) {
    chatStore.fetchModels()
  }
})
</script>

<style scoped>
.model-selection-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eaeaea;
}

.header h1 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.right-actions {
  display: flex;
  align-items: center;
}

.back-button {
  display: flex;
  align-items: center;
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.2s;
}

.back-button:hover {
  color: #1a73e8;
}

.models-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.model-card {
  border: 1px solid #eaeaea;
  border-radius: 8px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.model-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.model-card.active {
  border-color: #1a73e8;
  background-color: rgba(26, 115, 232, 0.05);
}

.model-info h3 {
  margin: 0 0 10px 0;
  font-size: 18px;
  color: #333;
}

.feature-tag {
  display: inline-block;
  background-color: #e8f0fe;
  color: #1a73e8;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-top: 5px;
}

.model-action {
  margin-top: 20px;
}

.select-btn {
  width: 100%;
  padding: 8px 16px;
  background-color: #1a73e8;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.select-btn:hover:not(:disabled) {
  background-color: #1557b0;
}

.select-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.no-models {
  grid-column: 1 / -1;
  text-align: center;
  padding: 40px;
  color: #666;
}
</style> 